package org.dromara.iot.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.websocket.dto.WebSocketMessageDto;
import org.dromara.common.websocket.holder.WebSocketSessionHolder;
import org.dromara.common.websocket.utils.WebSocketUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * 电梯保养记录
 *
 * @author lsl
 * @date 2024-06-14
 */
@SaIgnore
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/WebSocket")
public class IotWebSocketController extends BaseController {
    /**
     * 发送消息
     */
    @PostMapping("/sendMessage")
    public R<Void> sendMessage(@RequestBody WebSocketMessageDto webSocketMessageDto) {
        WebSocketUtils.publishMessage(webSocketMessageDto);
        return R.ok();
    }

    /**
     * 列出所有WebSocketKey
     */
    @GetMapping("/listSessionKeys")
    public R<Set<String>> listSessionKeys() {
        Set<String> sessionsAll = WebSocketSessionHolder.getSessionsAll();
        return R.ok(sessionsAll);
    }
}
